package com.web.magic.catcher;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.web.magic.catcher.model.SearchEnum;
import com.web.magic.catcher.model.Top;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ResultItemsCollectorPipeline;

@Service
public class TopGeter {

	public List<Top> getCurrentTop10(SearchEnum key){
		String urlStr = Processor.getUrl(key);
		List<Top> list = new ArrayList<>();
		if(urlStr == null){
			return list;
		}
		ResultItemsCollectorPipeline pp = new ResultItemsCollectorPipeline();
		Spider.create(Processor.get(key)).addPipeline(pp).test(urlStr);
		for(ResultItems item: pp.getCollected()){
			for(Object s: item.getAll().values()){
				list.add((Top) s);
			}
		}
		return list;
	}
}
